#!usr/bin/env python3

from PyQt5.QtWidgets import QWidget,QLabel,QLineEdit,QPushButton,QFileDialog
import os

class BrowseWidget(QWidget):
    def __init__(self,layoutBox,sizeW,sizeH,headText:str="",holderText:str="请浏览目录"):
        super().__init__()
        self.layoutBox = layoutBox
        self.resize(sizeW,sizeH)

        self.lblPath = QLabel()
        self.lblPath.setText(headText)
        #self.mainBox.addWidget(self.lblPath, curRow, 0)
        self.layoutBox.addWidget(self.lblPath,1)

        self.pathInput = QLineEdit() #Json保存目录输入框
        self.pathInput.setPlaceholderText(holderText)
        self.pathInput.textChanged.connect(self.__onPathInputChange)
        py = self.pathInput.height()-self.lblPath.height()
        self.pathInput.move(
            self.lblPath.x()+self.lblPath.width()-30, self.lblPath.y()-5)
        #self.mainBox.addWidget(self.pathInput, curRow,1)
        self.layoutBox.addWidget(self.pathInput,5)
        
        self.btnBrowse = QPushButton()
        self.btnBrowse.setText("浏览")
        self.btnBrowse.setToolTip("打开PC的浏览目录对话框")        
        self.btnBrowse.clicked.connect(self.__clickBtn)  # 注册点击事件
        #self.mainBox.addWidget(self.btnBrowse, curRow, 2)
        self.layoutBox.addWidget(self.btnBrowse,1)

        self.openBtn = QPushButton()
        self.openBtn.setText("打开")
        self.openBtn.setToolTip("直接打开所在目录")        
        self.openBtn.clicked.connect(self.__clickBtn)  # 注册点击事件
        #self.mainBox.addWidget(self.btnBrowse, curRow, 2)
        self.layoutBox.addWidget(self.openBtn,1)


    def __clickBtn(self, *arg):
        btn = self.sender()
        if btn==self.btnBrowse:
            self.__browse()
        elif btn==self.openBtn and os.path.isdir(self.pathInput.text()):
            os.startfile(self.pathInput.text()) #浏览目录
    def __browse(self):
        #print("点击按钮了....", arg)
        #fileName,fileType = QFileDialog.getOpenFileName(self,"选中模块根目录")
        fileName = QFileDialog.getExistingDirectory(
            self, "选中目录") #"E:/H5Game/Client/src/game/module"        

        self.setFocus()          
        print("BrowseWidget,fileName=",fileName)
        self.pathInput.setText(fileName)

    def __onPathInputChange(self):
        fileName = self.pathInput.text()

    def getPath(self):
        return self.pathInput.text()
    def setPath(self,path):
        self.pathInput.setText(path)
    